package com.lfs.market.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfs.market.exception.ServiceException;
import com.lfs.market.model.R;
import com.lfs.market.model.dto.SystemUserCreateDto;
import com.lfs.market.model.dto.SystemUserDto;
import com.lfs.market.model.dto.SystemUserPageDto;
import com.lfs.market.model.dto.SystemUserUpdateDto;
import com.lfs.market.model.entity.SystemUser;
import com.lfs.market.model.enums.ResultCode;
import com.lfs.market.service.SystemUserService;
import com.lfs.market.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 西尾ink
 * @version 1.0
 * @description TODO
 * @date 2024/2/18 21:54
 */
@RestController
@RequestMapping("/admin/user")
@SaCheckLogin
@SaCheckRole(value = {"system", "admin", "check"}, mode = SaMode.OR)
public class SystemUserAdminController {
    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private UserService userService;

    @GetMapping("/getUserInfo")
    public R<SystemUser> getUserInfo() {
        return systemUserService.getUserInfo();
    }

    @GetMapping("/page")
    public R<Page> getUserPageList(SystemUserPageDto userPageDto) {
        Page page = systemUserService.getUserPageList(userPageDto);
        return R.ok(page);
    }

    /**
     * 创建普通用户
     *
     * @param dto
     * @return
     */
    @PostMapping
    public R create(@RequestBody SystemUserCreateDto dto) {
        userService.create(dto);
        return R.ok();
    }

    /**
     * 批量删除普通用户
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    public R deleteUser(@PathVariable("ids") List<String> ids) {
        userService.deleteUser(ids);
        return R.ok();
    }


    /**
     * 修改普通用户
     *
     * @param dto
     * @return
     */
    @PutMapping
    public R updateUser(@RequestBody SystemUserUpdateDto dto) {
        userService.updateUser(dto);
        return R.ok();
    }

    /**
     * 管理员创建普通用户
     *
     * @param dto
     * @return
     */
    @PostMapping("/create")
    public R systemCreateUser(@RequestBody SystemUserDto dto) {
        systemUserService.systemCreateUser(dto);
        return R.ok();
    }

    /**
     * 管理员删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R delete(String id) {
        SystemUser systemUser = systemUserService.getById(id);
        if (ObjectUtil.isEmpty(systemUser)) throw new ServiceException(ResultCode.Fail);
        boolean remove = systemUserService.removeById(id);
        if (!remove)
            throw new ServiceException(ResultCode.DeleteError);
        return R.ok();
    }

    /**
     * 修改普通用户
     *
     * @param dto
     * @return
     */
    @PutMapping("/update")
    public R systemUpdateUser(@RequestBody SystemUserDto dto) {
        systemUserService.systemUpdateUser(dto);
        return R.ok();
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    public R logout() {
        Object loginId = StpUtil.getLoginId();
        StpUtil.logout(loginId);
        return R.ok();
    }
}
